package com.penta.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Excel 工具类
 */
public class ExcelHelper {

    /**
     * 读取数据列表
     * @param sourceFile
     * @return
     */
    public static List<List<Object>> readDataList(File sourceFile) {

        ExcelReader reader = ExcelUtil.getReader(sourceFile);

        List<List<Object>> dataList = reader.read();

        return dataList;

    }

    /**
     * 获取excel的所有图片列表 - xlsx 文件
     * @param sourceFile
     * @return
     *      字节数组列表
     */
    public static List<byte[]> getPictureDataList(File sourceFile) {

        List<byte[]> pictureDataList = new ArrayList<>();

        List<XSSFShape> shapeList = new ArrayList<>();
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(sourceFile);
            XSSFSheet sheet = workbook.getSheetAt(0);

            XSSFDrawing xssfDrawing = sheet.getDrawingPatriarch();

            shapeList = xssfDrawing.getShapes();

        } catch (Exception e) {
            //e.printStackTrace();
        }

        /**
         * 拿回所有属于 XSSFPicture 的列表
         */
        if(CollectionUtil.isNotEmpty(shapeList)) {
            pictureDataList = shapeList.stream().filter(shape->shape instanceof XSSFPicture).map(shape->{
                XSSFPicture xssfPicture =  (XSSFPicture)shape;
                return xssfPicture.getPictureData().getData();
            }).collect(Collectors.toList());
        }
        return pictureDataList;
    }

    /**
     * 获取excel的所有图片列表 - xls 文件
     * @param sourceFile
     * @return
     *      图片字节数组列表
     */
    public static List<byte[]> getPictureDataListForXls(File sourceFile) {

        /**
         * 图片字节数组
         */
        List<byte[]> pictureDataList = new ArrayList<>();

        List<HSSFShape> shapeList = new ArrayList<>();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(sourceFile));

            HSSFSheet sheet = workbook.getSheetAt(0);

            HSSFPatriarch pat = sheet.getDrawingPatriarch();

            shapeList = pat.getChildren();

        } catch (Exception e) {
            //e.printStackTrace();
        }

        /**
         * 拿回所有属于 XSSFPicture 的列表
         */
        if(CollectionUtil.isNotEmpty(shapeList)) {

            pictureDataList = shapeList.stream().filter(shape->shape instanceof HSSFPicture).map(shape->{
                HSSFPicture hssfPicture =  (HSSFPicture)shape;
                return hssfPicture.getPictureData().getData();
            }).collect(Collectors.toList());
        }
        return pictureDataList;
    }


    /**
     * 读取文件中，所有的图片数据
     * @param sourceFile
     */
    public static List<? extends PictureData> readPictureList(File sourceFile) {

        ExcelReader reader = ExcelUtil.getReader(sourceFile);



        List<? extends PictureData> pictureList = reader.getWorkbook().getAllPictures();

        return pictureList;

    }


    public static List<XSSFShape> readShapeList(File sourceFile) {
        List<XSSFShape> shapeList = null;
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(sourceFile);

            XSSFSheet sheet = workbook.getSheetAt(0);

            XSSFDrawing drawing = sheet.createDrawingPatriarch();

            shapeList = drawing.getShapes();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return shapeList;
    }


}
